/*
 * ************************************************
 *
 *              STM32 blink gcc demo
 *
 *  CPU: STM32F103C8
 *  PIN: PC13
 *
 * ************************************************
 */

#include "stm32f10x.h"
#include "w25qxx.h"
#include "usart.h"
#include <stdio.h>
#include "oled.h"

#include "log.h"

/* GPIO引脚定义 */
// LED引脚配置：PC13
#define LED_PERIPH RCC_APB2Periph_GPIOC    // LED所在外设时钟
#define LED_PORT GPIOC                      // LED所在GPIO端口
#define LED_PIN GPIO_Pin_13                 // LED引脚号

// 蜂鸣器引脚配置：PA8
#define BEEP_PERIPH RCC_APB2Periph_GPIOA   // 蜂鸣器所在外设时钟
#define BEEP_PORT GPIOA                     // 蜂鸣器所在GPIO端口
#define BEEP_PIN GPIO_Pin_8                 // 蜂鸣器引脚号

/**
 * @brief 简单的延时函数
 * @param x 延时时间（相对值）
 */
void delay(int x)
{
    for (int i = 0; i < x; i++) {
        for (int j = 0; j < 1000; j++)
            __NOP();                        // 空操作指令
    }
}

void BEEP_Init(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    
    RCC_APB2PeriphClockCmd(BEEP_PERIPH, ENABLE);
    
    GPIO_InitStructure.GPIO_Pin = BEEP_PIN;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;  // 推挽输出
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(BEEP_PORT, &GPIO_InitStructure);
    
    GPIO_ResetBits(BEEP_PORT, BEEP_PIN);
}

void BEEP_Control(uint8_t status)
{
    if(status)
        GPIO_SetBits(BEEP_PORT, BEEP_PIN);
    else
        GPIO_ResetBits(BEEP_PORT, BEEP_PIN);
}

int main()
{
    GPIO_InitTypeDef gpioDef;
    
    // 初始化串口1,波特率115200
    USART1_Init(115200);
    printf("System Init...\r\n");
    
    RCC_APB2PeriphClockCmd(LED_PERIPH, ENABLE);
    gpioDef.GPIO_Mode = GPIO_Mode_Out_PP;
    gpioDef.GPIO_Pin = LED_PIN;
    gpioDef.GPIO_Speed = GPIO_Speed_10MHz;
    GPIO_Init(LED_PORT, &gpioDef);
    
    BEEP_Init();
    
    // 初始化OLED
    OLED_Init();
    OLED_Clear();
    delay(1000);
    
    // 在OLED第一页显示字母'A'的点阵数据
    OLED_SetPosition(0, 0);
    OLED_WriteData(0x7C);
    OLED_WriteData(0x12);
    OLED_WriteData(0x11);
    OLED_WriteData(0x12);
    OLED_WriteData(0x7C);
    
    W25QXX_Init();
    uint16_t flash_id = W25QXX_ReadID();
    printf("W25QXX ID: 0x%04X\r\n", flash_id);
    
    if(flash_id == 0xEF16) {
        uint8_t write_buf[4] = {0x11, 0x22, 0x33, 0x44};
        uint8_t read_buf[4] = {0};
        
        W25QXX_Write(write_buf, 0, 4);
        printf("Write Data: 0x%02X 0x%02X 0x%02X 0x%02X\r\n", 
               write_buf[0], write_buf[1], write_buf[2], write_buf[3]);
        
        W25QXX_Read(read_buf, 0, 4);
        printf("Read Data: 0x%02X 0x%02X 0x%02X 0x%02X\r\n",
               read_buf[0], read_buf[1], read_buf[2], read_buf[3]);
        
        // OLED_Clear();
        if(read_buf[0] == 0x11 && read_buf[1] == 0x22 && 
           read_buf[2] == 0x33 && read_buf[3] == 0x44) {
            printf("Flash Test OK!\r\n");
            // ... OLED显示代码 ...
        } else {
            printf("Flash Test Failed!\r\n");
            // ... OLED显示代码 ...
        }
    }

    
    LOGI("System Init OK!");
    while (1) {
        LOGI("GPIO_WriteBit");
        GPIO_WriteBit(LED_PORT, LED_PIN, (BitAction)!GPIO_ReadInputDataBit(LED_PORT, LED_PIN));
        BEEP_Control(1);
        delay(100);
        BEEP_Control(0);
        delay(1000);
    }
}
